/*
 * DateTimeService.java
 * 
 * Created: Aug 7, 2015
 * 
 * Copyright (c) ExpertPB 2015
 * All information contained herein is, and remains the property of
 * ExpertPB and its suppliers, if any.
 */
package com.emex.app.stuff.utils;

/**
 *
 * @author Sergey
 */
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This service is required to set the time zone to UTC. Also this service
 * provides a method that returns the time, taking into account time zone.
 *
 * @author Alexey Pashkin
 */
public class DateTimeService {

    private static final Logger logger = LoggerFactory.getLogger(DateTimeService.class);
    private static DateTimeService instance = new DateTimeService();
    private TimeZone clientTimeZone;

    /**
     * Private constructor. Do not create objects of this type directly.
     */
    private DateTimeService() {
        clientTimeZone = TimeZone.getDefault();
        logger.info("Set time zone to UTC");
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    }

    /**
     * Get default DateTimeService.
     *
     * @return the single DateTimeService object
     */
    public static DateTimeService getInstance() {
        return instance;
    }

    public TimeZone getClientTimeZone() {
        return clientTimeZone;
    }

    public Date getDate() {
        Date date = new Date();
        date.setTime(date.getTime() + clientTimeZone.getRawOffset());
        return date;
    }

    public Calendar getCalendar() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDate());
        return calendar;
    }

    public Date getDateWithTimeZone(Date date) {
        if (date == null) {
            return null;
        }
        Date dateWithTimeZone = new Date();
        dateWithTimeZone.setTime(date.getTime() + clientTimeZone.getRawOffset());
        return dateWithTimeZone;
    }
}
